(function($) {
	var pageNo;
	var pageSize;
	var data;
	var sourceHtml;
	$.fn.getCheckData = function(index){
		if(index) return data[index];
		var $this = $(this);
		var res = new Array();
		var radios = $this.find("tbody td input[type='checkbox']");
		var l = radios.length;
		for(var i = 0 ; i < l ; i ++){
			if($(radios[i]).is(":checked")) res.push(data[i]);
		}
		return res;
	}
    $.fn.getAllTableData = function () {
		return data;
    }
	$.fn.ECDATableTable = function() {
		var $this = $(this);
		var paramdata = {};
		var dataAction = $this.attr("dataAction");
		if(!pageNo){
			pageNo = 1;
		}
		paramdata["pageNo"] = pageNo;
		var operate = $this.attr("operate") == "true";
	    var t = $("#" + $this.attr("bindForm")).serializeArray();
	    $.each(t, function() {
	    	paramdata[this.name] = this.value;
	    });
	    var url = baseUrl + dataAction;
	    jQuery.ajax({
	        url: url,
	        dataType: 'json',
	        data: paramdata,
	        timeout: 60000,
	        cache: false,
	        async: false,
	        success: function(page) {
	        	data = page.list;
	        	sourceHtml = sourceHtml?sourceHtml:$this.find("table").eq(0).prop('outerHTML');
	        	$this.empty();
	        	for(var i = 0 ; i < data.length ; i++){
	        		$this.append(sourceHtml);
	        		var $html = $this.find("table").eq(i);
	        		$html.find("thead").eq(0).append("<input type='hidden' name = 'index' value = '" + i + "'>");
	        		$html.find("tbody").eq(0).append("<input type='hidden' name = 'index' value = '" + i + "'>");
	        		var $ths = $html.find("span[type='table']");
	        		var op = $($html).find("td[type='op']");
	        		$.each($ths,function(){
	        		   console.log(this);
	        			var type = $(this).attr("dataType");
	        			var names = $(this).attr("name").split(".");
						var value = data[i][names[0]]?names.length > 1?data[i][names[0]][names[1]]?data[i][names[0]][names[1]]:"":data[i][names[0]]?data[i][names[0]]:"":"";
						if(type == "Integer"){
	        				value = value == 0?"0":value;
	        			}else if(type == "dict"){
	        				var dictType = $(this).attr("dictType");
							value = getDictLable(dictType,value);
	        			}
						$(this).text(value);
	        		})
	        		$.each(op,function(){
	        			var $this = $(this);
	        			if(opCallBack&&typeof opCallBack == "function"){
	        				opCallBack($this,data[i]);
	    			    }	        			
	        		})
	        	}
	        	if($this.attr("pagenation") != "false"){
					$this.append(page.html);
				}
			    $this.next().find("a").click(function(){
			    	var dataPage = $(this).attr("data-page");
			    	var text = $(this).text();
			    	if(!isNaN(dataPage)){
			    		pageNo = $(this).attr("data-page");
			    	}else if(pageNo>1&&dataPage == "pre"){
			    		pageNo--
			    	}else if(pageNo<pageCount&&dataPage == "next"){
			    		pageNo++;
			    	}else if(text == "跳转"){
			    		dataPage = $(this).prev().val();
			    		if(1 <= dataPage <= pageCount){
			    			pageNo = dataPage;
			    		}else{
			    			return;
			    		}
			    	}else{
			    		return;
			    	}
			    	$("#" + $this.attr("bindClick")).click();
			    });
			    if(tableCallBack&&typeof tableCallBack == "function"){
			    	tableCallBack($this);
			    }
	        }
	    });
	};  
})(jQuery);

function getDictValue(status,type,callback){
	var data = {};
	data.values = status;
	data.type = type;
	data.defaultValue='';
	jQuery.ajax({
        url: baseUrl+'/dict/getDictLabels',
        timeout: 60000,
        cache: false,
        data:data,
        success: function(datas) {
        	callback(datas);
        }
     })
}

$(document).ready(function() {
	var $table = $('div[dataTableTable="true"]');
	console.log($table[0]);
	$table.each(function(){
		var $this = $(this);
		var table = $this.ECDATableTable();
		var $tableClick = $("#" + $this.attr("bindClick"));
		$tableClick.click(function(){
			$this.ECDATableTable();
		});
	})
	
	
})